package com.itsci.assignment1;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.HashMap;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.AdapterView.OnItemClickListener;

public class ShowMidicalHistory extends Activity {
	private String intentName;
	private ListView lv;
	private String path = "/mnt/sdcard/HistoryMidical/";
	private String pathFile;
	private String[] listview_array;
	private TextView tv;
	private String fileRead[];
	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.show_midical);
		Intent intent = getIntent();
		intentName = intent.getStringExtra("name");
		tv = (TextView)findViewById(R.id.textView_show_midicalname);
		tv.setText(intentName);
		lv = (ListView) findViewById(R.id.listView1);
		pathFile = path + intentName.replace(" ", "") + ".txt";
		listview_array = CreateArray();
		ArrayList<HashMap<String, String>> MyArrList = new ArrayList<HashMap<String, String>>();
		for(int i =0;i<listview_array.length;i++){
			HashMap<String, String> map ;
			map = new HashMap<String, String>();
			map.put("Id",listview_array[i].split(",")[0]);
			map.put("Date",listview_array[i].split(",")[1]);
			map.put("Time",listview_array[i].split(",")[2]);
			map.put("Summary",listview_array[i].split(",")[3]);
			MyArrList.add(map);
		}
//		fileRead = ReadFileForSave();
		SimpleAdapter sAdap;
		sAdap = new SimpleAdapter(ShowMidicalHistory.this, MyArrList,
				R.layout.custom_midical_history, new String[] { "Id",
						"Date", "Time", "Summary" }, new int[] {
						R.id.textView_cus_midical_id,
						R.id.textView_cus_midical_date,
						R.id.textView_cus_midical_time,
						R.id.textView_cus_midical_summary });
		lv.setAdapter(sAdap);

		lv.setOnItemClickListener(new OnItemClickListener() {
			@Override
			public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
					long arg3) {
				
				Intent gotoSchedule = new Intent(ShowMidicalHistory.this,ScheduleDetail.class);
				gotoSchedule.putExtra("no", arg2);
				gotoSchedule.putExtra("name",intentName);
				gotoSchedule.putExtra("date",listview_array[(int) lv.getItemIdAtPosition(arg2)].split(",")[1]);
				gotoSchedule.putExtra("time",listview_array[(int) lv.getItemIdAtPosition(arg2)].split(",")[2]);
				gotoSchedule.putExtra("summary",listview_array[arg2].split(",")[3]);
				gotoSchedule.putExtra("detail",listview_array[arg2].split(",")[4]);
				startActivity(gotoSchedule);
				

			}
		});

	}

	public String[] CreateArray() {
		ArrayList<String> test = new ArrayList<String>();
		try {
			File myFile = new File(pathFile);
			if (!myFile.exists()) {
				try {
					myFile.createNewFile();
				} catch (IOException e1) {
					e1.printStackTrace();
				}
			}
			FileInputStream fIn = new FileInputStream(myFile);
			BufferedReader myReader = new BufferedReader(new InputStreamReader(
					fIn));
			String aDataRow = "";
			while ((aDataRow = myReader.readLine()) != null) {
				test.add(aDataRow);
			}
			myReader.close();

		} catch (Exception e) {
			Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_LONG)
					.show();
		}
		return (String[]) test.toArray(new String[test.size()]);
	}

	@Override
	public boolean onCreateOptionsMenu(Menu menu) {
		menu.add(0, 1, 0, "Add Schedule Medical");
		menu.add(0, 2, 0, "Delete Schedule Medical");
		return super.onCreateOptionsMenu(menu);

	}
	
	public String[] ReadFileForSave() {
		ArrayList<String> temp = new ArrayList<String>();
		try {
			File myFile = new File(path);
			FileInputStream fIn = new FileInputStream(myFile);
			BufferedReader myReader = new BufferedReader(new InputStreamReader(
					fIn));
			String aDataRow = "";
			@SuppressWarnings("unused")
			int i=0;
			while ((aDataRow = myReader.readLine()) != null) {
				temp.add(aDataRow);
				i++;
			}
			myReader.close();
		} catch (Exception e) {
			Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT)
					.show();
		}
		return (String[]) temp.toArray(new String[temp.size()]);
	}	

}
